package site.hanzhe.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import site.hanzhe.constant.CrowdConstant;
import site.hanzhe.exception.AccessForbiddenException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    // 请求到达Controller之前进行拦截
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
        HttpSession session = req.getSession();
        // 如果session中没有存值，就直接抛出异常
        if ( session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN) == null )
            throw new AccessForbiddenException(CrowdConstant.MESSAGE_ACCESS_FORBIDEN);
        // 拦截器返回一个boolean值，当值为true时为通过
        return true;
    }

}